package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.MachineBuy;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.MachineBuyRepository;
import co.yixiang.modules.shop.service.MachineBuyService;
import co.yixiang.modules.shop.service.dto.MachineBuyDto;
import co.yixiang.modules.shop.service.dto.MachineBuyQueryCriteria;
import co.yixiang.modules.shop.service.mapper.MachineBuyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "machineBuy")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MachineBuyServiceImpl implements MachineBuyService {

    private final MachineBuyRepository machineBuyRepository;

    private final MachineBuyMapper machineBuyMapper;

    public MachineBuyServiceImpl(MachineBuyRepository machineBuyRepository, MachineBuyMapper machineBuyMapper) {
        this.machineBuyRepository = machineBuyRepository;
        this.machineBuyMapper = machineBuyMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(MachineBuyQueryCriteria criteria, Pageable pageable){
        Page<MachineBuy> page = machineBuyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(machineBuyMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<MachineBuyDto> queryAll(MachineBuyQueryCriteria criteria){
        return machineBuyMapper.toDto(machineBuyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public MachineBuyDto findById(Integer id) {
        MachineBuy machineBuy = machineBuyRepository.findById(id).orElseGet(MachineBuy::new);
        ValidationUtil.isNull(machineBuy.getId(),"MachineBuy","id",id);
        return machineBuyMapper.toDto(machineBuy);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public MachineBuyDto create(MachineBuy resources) {
        return machineBuyMapper.toDto(machineBuyRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(MachineBuy resources) {
        MachineBuy machineBuy = machineBuyRepository.findById(resources.getId()).orElseGet(MachineBuy::new);
        ValidationUtil.isNull( machineBuy.getId(),"MachineBuy","id",resources.getId());
        machineBuy.copy(resources);
        machineBuyRepository.save(machineBuy);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            machineBuyRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<MachineBuyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MachineBuyDto machineBuy : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", machineBuy.getUserId());
            map.put("标题", machineBuy.getTitle());
            map.put("品牌", machineBuy.getBrandId());
            map.put("类型id", machineBuy.getTypeId());
            map.put("详细内容", machineBuy.getMessage());
            map.put("年限", machineBuy.getYears());
            map.put("阅读次数", machineBuy.getCountNumber());
            map.put("创建时间", machineBuy.getCreateTime());
            map.put("是否删除", machineBuy.getDelFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}